package com.mysteel.cyx.demo.biz.export;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Vicky(陈烟星)
 * @date 2024/6/19 09:47
 */
public class ExcelUtil {
    private static final String FONT_NAME = "宋体";
    private static final short FONT_SIZE = 11;


    // 写入题行
    @SafeVarargs
    public static void writeTitle(ExcelWriter excelWriter, WriteSheet writeSheet, List<String>... lists) {
        List<List<String>> header = new ArrayList<>(Arrays.asList(lists));
        excelWriter.write(header, writeSheet);
    }


    // 写入加粗靠左标题行
    @SafeVarargs
    public static void writeBoldLeftAlignedTitle(ExcelWriter excelWriter, WriteSheet writeSheet, Sheet sheet, List<String>... lists) {
        writeTitle(excelWriter, writeSheet, lists);
        // 获取样式
        Workbook workbook = sheet.getWorkbook();
        // 设置单元格样式
        Row row = sheet.getRow(sheet.getLastRowNum());
        if (row != null) {
            CellStyle boldLeftStyle = createBoldLeftCellStyle(workbook);
            for (Cell cell : row) {
                cell.setCellStyle(boldLeftStyle);
            }
        }
    }

    // 写入数据
    public static <T> void writeAlignedTData(List<T> data, Class<T> clazz, ExcelWriter excelWriter, WriteSheet writeSheet, int tableNo, boolean relativeHeadRowIndex) {
        writeData(data, clazz, excelWriter, writeSheet, tableNo, relativeHeadRowIndex);

        WriteSheetHolder writeSheetHolder = excelWriter.writeContext().writeSheetHolder();
        Sheet sheet = writeSheetHolder.getSheet();
        // 设置单元格样式
        Row row = sheet.getRow(sheet.getLastRowNum());
        if (row != null) {
            CellStyle boldLeftStyle = createCellStyle(sheet.getWorkbook(), FONT_NAME, (short) 10, false, HorizontalAlignment.CENTER);
            ;
            for (Cell cell : row) {
                cell.setCellStyle(boldLeftStyle);
            }
        }
    }

    public static <T> void writeData(List<T> data, Class<T> clazz, ExcelWriter excelWriter, WriteSheet writeSheet, int tableNo, boolean relativeHeadRowIndex) {
        WriteTable aTable;
        if (relativeHeadRowIndex) {
            aTable = EasyExcel.writerTable(tableNo).head(clazz).needHead(true).relativeHeadRowIndex(0).build();
        } else {
            aTable = EasyExcel.writerTable(tableNo).head(clazz).build();
        }
        excelWriter.write(data, writeSheet, aTable);
    }

    // 创建加粗靠左单元格样式
    public static CellStyle createBoldLeftCellStyle(Workbook workbook) {
        return createCellStyle(workbook, FONT_NAME, FONT_SIZE, true, HorizontalAlignment.LEFT);
    }

    public static CellStyle createBoldCenterCellStyle(Workbook workbook, short fontHeightInPoints, boolean isBold) {
        return createCellStyle(workbook, FONT_NAME, fontHeightInPoints, isBold, HorizontalAlignment.CENTER);
    }

    public static CellStyle createBoldCenterCellStyle(Workbook workbook) {
        return createCellStyle(workbook, FONT_NAME, FONT_SIZE, true, HorizontalAlignment.CENTER);
    }

    public static CellStyle createCellStyle(Workbook workbook, String fontName, short fontHeightInPoints, boolean isBold, HorizontalAlignment alignment) {
        CellStyle boldStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName(fontName);
        font.setFontHeightInPoints(fontHeightInPoints);
        font.setBold(isBold);
        boldStyle.setFont(font);
        boldStyle.setAlignment(alignment);
        boldStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return boldStyle;
    }



    public static void main(String[] args) {
        String s = null;
        if( (s==null)|(s.length()==0) ){

        }
    }

}
